<?php

namespace app\admin\model\listening;

use think\Model;
use fast\Tree;
use think\Cache;


class Listeningclass extends Model
{

    

    

    // 表名
    protected $name = 'listeningclass';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];



    protected static function init()
    {
        self::afterInsert(function ($row) {
//            此处为写入weigh权重代码
//            $pk = $row->getPk();
//            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
        self::afterWrite(function ($row) {
            Cache::rm('listeningclass');
        });
    }

    public function getTree(){
        $data=Cache::get('prefer');
        if(empty($data)){
            $tree = Tree::instance();
            $tree->init(collection($this->order('id desc')->select())->toArray(), 'listeningclass_id','');
            $preferlist = $tree->getTreeList($tree->getTreeArray(0), 'title');
            $data = [0 => ['id' => '', 'name' => '请选择','listeningclass_id'=>'-1']];
            foreach ($preferlist as $k => $v)
            {
                $data[$v['id']] = $v;
            }
            Cache::set('listeningclass',$data);
        }
        return $data;
    }
    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




}
